package com.example.espressotests

import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_my_web_view.*


class MyWebViewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_web_view)

        val url = intent.getStringExtra("url")
        setJS(web_view)
        web_view.loadUrl(url)
    }

    /**
     * 设置相关参数
     *
     * @param webView
     */
    private fun setJS(webView: WebView) {
        //设置支持js
        val webSettings = webView.settings
        webSettings.defaultTextEncodingName = "UTF-8"
        webSettings.javaScriptEnabled = true //设置支持js脚本
        webSettings.allowFileAccess = true //设置允许访问文件数据
        webSettings.builtInZoomControls = false //支持缩放
        webSettings.lightTouchEnabled = true
        webSettings.setSupportZoom(true)
        webView.isHapticFeedbackEnabled = false
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView,
                url: String
            ): Boolean { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url)
                return true
            }
        }
    }
}
